package com.mreko.chatroot;

import java.util.List;

import com.mreko.pojo.ChatBean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ChatAdapter extends BaseAdapter {
	private List<ChatBean> chatBeans;
	private Context context;
	private LayoutInflater inflater;
	private ImageView imageView;
	private TextView messageView, timeView;

	public ChatAdapter(List<ChatBean> chatBeans, Context context) {
		super();
		this.chatBeans = chatBeans;
		this.context = context;
	}

	@Override
	public int getCount() {
		return chatBeans.size();
	}

	@Override
	public Object getItem(int position) {
		return chatBeans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ChatBean chatBean = chatBeans.get(position);
		inflater = LayoutInflater.from(context);
		if (chatBean.isLeft()) {
			convertView = inflater.inflate(
					com.mreko.chatroot.R.layout.chat_list_left, null);
			imageView = (ImageView) convertView.findViewById(R.id.chat_left);
			messageView = (TextView) convertView
					.findViewById(R.id.message_chat_left);
			timeView = (TextView) convertView
					.findViewById(R.id.sendtime_chat_left);
			imageView.setImageResource(R.drawable.picted);
			messageView.setText(chatBean.getContent());
			timeView.setText(chatBean.getTime());
		} else {
			convertView = inflater.inflate(
					com.mreko.chatroot.R.layout.chat_list_right, null);
			imageView = (ImageView) convertView.findViewById(R.id.chat_right);
			messageView = (TextView) convertView
					.findViewById(R.id.message_chat_right);
			timeView = (TextView) convertView
					.findViewById(R.id.sendtime_chat_right);
			imageView.setImageResource(R.drawable.pic1);
			messageView.setText(chatBean.getContent());
			timeView.setText(chatBean.getTime());
		}

		return convertView;
	}

}
